MyModules.define('images',[],function(){
	class Child extends WidgetObject {
	    constructor() {
	        super(); // 调用父类构造函数
	        // 子类可以添加自己的属性或方法
	        this.name = '圖片組';
		    this.datas.name = this.name;

		    this.getHtml = function () {
				var str = '<div class="wrap-widget imagewsWidget" data-id="'+this.datas.id+'"  data-original-title="'+this.datas.name+'" data-toggle="tooltip">';
				if(!empty(this.datas.value['content']) && this.datas.value['content'].length){
					for(var i=0; i<this.datas.value['content'].length; i++ )
					str += '<div class="item"><img src="'+formatUrl(this.datas.value['content'][i])+'" /></div>';
				}
				str += '</div>';
				return str;
			};
			this.inputTemplate = function(id, value){
				var str = '<button type="button" class="btn btn-success" data-type="addrow" data-id="'+this.datas.id+'">添加圖片</button><div class="x_content">';
				if(this.datas.value['content']){
					row_index = 0;
					for(var i=0; i < this.datas.value['content'].length; i++){
						str += renderImageSelecterRow(this.datas.id+'-'+i, this.datas.value['content'][i]);
						row_index++;
					}
				}
				str += '	</div>';
				return str;
	        };
	        this.saveData = function(){
	        	var values = $("#myModal form").serializeArray();
				var contents = []
				for(var x of values){
					if(x['name'] == 'content'){
						contents.push(x['value']);
					}
				}
				this.datas.value = arrayToObject(values);
				this.datas.value['content'] = contents
	        }
	    }
	}
	return new Child();
});
var row_index = 0
$(document).on("click", "[data-type='addrow']", function(){
	var id = $(this).data("id");
	
	i = row_index;
	
	str = renderImageSelecterRow(id+"-"+i);
	$("#home > .x_content").append(str);
	row_index++;
});

$(document).on("click", "[data-type='select-delete']", function(){
	$(this).parents(".form-group").eq(0).remove();
});

function renderImageSelecterRow(id, value){
	value = typeof(value) != "undefined" ? value : '';
	var url = buildUrl(urlInfo.upload.iframe,{
		type:0,
		relative_url:1,
		akey:apkey,
		field_id:'input-'+id,
	});
	var shtml = `<div class="form-group "><div class="input-group">
	<input class="form-control" name="content" id="input-${id}" value="${value}">
	<div class="input-group-btn">
	<a href="${url}" class="btn btn-primary btn-rf-image" img-fancybox data-id="input-${id}">選擇圖片</a>
	<button type="button" class="btn btn-danger" data-type="select-delete" data-id="input-${id}"><i class="glyphicon glyphicon-trash"></i></button>
	</div>
	</div></div>`;
	return shtml;
}